package com.xintonghua.bussiness.ui.fragment.cube.turnover;

import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.StockAdapter;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.CubeBean;
import com.xintonghua.bussiness.bean.ServiceOrProductListBean;
import com.xintonghua.bussiness.bean.ServiceOrProductResponse;
import com.xintonghua.bussiness.bean.ZhiChu2ListBean;
import com.xintonghua.bussiness.bean.ZhiChu2ListResponse;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

//  String type = "0";
//
//@Override
//protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_service_or_product_list);
//
//        }
public class ServiceOrProductListActivity extends BaseActivity implements XRecyclerView.LoadingListener {

    @BindView(R.id.recycler_club_card)
    XRecyclerView recyclerClubCard;
    private StockAdapter adapter;
    List<CubeBean> mlist;

    String type = "0";//0服务列表 1产品列表  2支出列表（新增）
    String starttime = null;
    String endtime = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_or_product_list);
        ButterKnife.bind(this);
        type = JumpUtils.getString(this, "type");
        starttime = JumpUtils.getString(this, "starttime");
        endtime = JumpUtils.getString(this, "endtime");

        onSimpleActionBar();
        switch (type) {
            case "0":
                setSimpleActionBar("服务列表");
                break;
            case "1":
                setSimpleActionBar("产品列表");
                break;
            case "2":
                setSimpleActionBar("支出列表");
                break;
        }

        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        mlist = new ArrayList<>();
        RefreshUtils.initList(this, recyclerClubCard);
        recyclerClubCard.setPullRefreshEnabled(true);
        recyclerClubCard.setLoadingMoreEnabled(true);

        adapter = new StockAdapter(this, mlist);
        switch (type) {
            case "0":
                adapter.setType(0);
                break;
            case "1":
                adapter.setType(1);
                break;
            case "2":
                adapter.setType(2);
                break;
        }

        adapter.setOnItemsClistener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                CubeBean bean = adapter.getList().get(i);
                HashMap<String, String> map = new HashMap<>();
                map.put("type", type);
                map.put("starttime", starttime);
                map.put("endtime", endtime);
                JumpUtils.jumpto(ServiceOrProductListActivity.this, TurnoverActivity.class, (Serializable) bean, map);
            }
        });
        recyclerClubCard.setAdapter(adapter);
        recyclerClubCard.setLoadingListener(this);
        onRefresh();
    }

    @Override
    public void onRefresh() {
        page = 1;
        switch (type) {
            case "0":
                HttpCent.getInstance(this).getServiceList(this, starttime, endtime, MyState.requestOne);
                break;
            case "1":
                HttpCent.getInstance(this).getProjectList(this, starttime, endtime, MyState.requestOne);
                break;

            case "2":
                HttpCent.getInstance(this).getZhichuList(this, starttime, endtime, MyState.requestTwo);
                break;
        }

    }

    int page = 1;

    @Override
    public void onLoadMore() {
        switch (type) {
            case "0":
                HttpCent.getInstance(this).getServiceList(this, starttime, endtime, MyState.requestOne);
                break;
            case "1":
                HttpCent.getInstance(this).getProjectList(this, starttime, endtime, MyState.requestOne);
                break;
            case "2":
                HttpCent.getInstance(this).getZhichuList(this, starttime, endtime, MyState.requestTwo);
                break;
        }
        page++;
    }


    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        try {
            switch (order) {
                case MyState.requestOne:
                    ServiceOrProductResponse response = JSONObject.parseObject(data.toString(), ServiceOrProductResponse.class);
                    List<ServiceOrProductListBean> beans = response.getList();

                    if (page == 1) {
                        mlist.clear();
                    }
                    List<CubeBean> beanss = new ArrayList<>();
                    for (ServiceOrProductListBean bean : beans) {
//                        CubeBean bean = new CubeBean(R.drawable.cube_product, beans.get(i).getName(), beans.get(i).getTotal(), beans.get(i).getCount(), false);
                        beanss.add(new CubeBean(R.drawable.cube_product, bean.getId(), bean.getName(), bean.getTotal(), bean.getCount(), false));
                    }

                    mlist.addAll(beanss);
                    adapter.notifyDataSetChanged();
                    recyclerClubCard.loadMoreComplete();
                    recyclerClubCard.refreshComplete();
                    break;
                case MyState.requestTwo:
                    ZhiChu2ListResponse response1=JSONObject.parseObject(data.toString(),ZhiChu2ListResponse.class);
                    List<ZhiChu2ListBean> beans2 = response1.getList();

                    if (page == 1) {
                        mlist.clear();
                    }
                    List<CubeBean> beanss2 = new ArrayList<>();
                    for (ZhiChu2ListBean bean : beans2) {
//                        CubeBean bean = new CubeBean(R.drawable.cube_product, beans.get(i).getName(), beans.get(i).getTotal(), beans.get(i).getCount(), false);
                        beanss2.add(new CubeBean(R.drawable.cube_product, bean.getId(), bean.getTitle(), bean.getMoney(), "0", false));
                    }

                    mlist.addAll(beanss2);
                    adapter.notifyDataSetChanged();
                    recyclerClubCard.loadMoreComplete();
                    recyclerClubCard.refreshComplete();
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
